package com.yyc.system.controller;
import com.yyc.core.Result;
import com.yyc.core.ResultGenerator;
import com.yyc.system.model.TClass;
import com.yyc.system.service.ITClassService;

import tk.mybatis.mapper.entity.Condition;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
* Created by yyc on 2018/12/26.
*/
@RestController
@RequestMapping("/t/class")
public class TClassController {
    @Autowired
    private ITClassService tClassService;

    @RequestMapping("/add")
    public Result add(@RequestBody TClass tClass) {
        tClassService.save(tClass);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/delete")
    public Result delete(@RequestParam String ids) {
        tClassService.deleteByIds(ids);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/update")
    public Result update(@RequestBody TClass tClass) {
        tClassService.update(tClass);
        return ResultGenerator.genSuccessResult();
    }

    @RequestMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        TClass tClass = tClassService.findById(id);
        return ResultGenerator.genSuccessResult(tClass);
    }

    @RequestMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<TClass> list = tClassService.findAll();
        PageInfo<TClass> pageInfo = new PageInfo<TClass>(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
    
    //*************************************************************
    
    @RequestMapping("/userClass")
    public Result userClass(Integer userId){
    	List<TClass> tClass = tClassService.queryUserClass(userId);
		return ResultGenerator.genSuccessResult(tClass);
    }
    
    @RequestMapping("/queryClassByName")
    public Result queryClassByName(String className){
    	Condition condition = new Condition(TClass.class);
    	condition.createCriteria().andEqualTo("className", className);
    	List<TClass> list = tClassService.findByCondition(condition);
		return ResultGenerator.genSuccessResult(list);
    }
}
